package javabasic.singleton.lanhanshi;

import java.util.concurrent.*;

public class Singleton07 {
    private static volatile Singleton07 INSTANCE;
    private Singleton07(){}
    public static Singleton07 getINSTANCE(){
        if (INSTANCE==null){
            synchronized (Singleton07.class){
                if (INSTANCE==null){
                    INSTANCE=new Singleton07();
                }
            }
        }
        return INSTANCE;
    }
}

class Singleton07Test{
    public static void main(String[] args) {
        //Singleton07 instance = Singleton07.getINSTANCE();
        //Singleton07 instance1 = Singleton07.getINSTANCE();
        //System.out.println("比较");
        //System.out.println(instance==instance1);

        Callable<Singleton07> singleton07Callable = new Callable<Singleton07>() {
            @Override
            public Singleton07 call() throws Exception {
                return Singleton07.getINSTANCE();
            }
        };

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);
        Future<Singleton07> future = fixedThreadPool.submit(singleton07Callable);
        Future<Singleton07> future1 = fixedThreadPool.submit(singleton07Callable);
        try {
            Singleton07 singleton07 = future.get();
            Singleton07 singleton071 = future1.get();
            System.out.println(singleton07==singleton071);
            fixedThreadPool.shutdown();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
}

